<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      @keyup.enter.native="dataFormSubmit()"
      label-width="80px"
    >
      <el-form-item label="上级机构" prop="parentName" v-if="!isRoot">
        <el-popover ref="dtListPopover" placement="bottom-start" trigger="click">
          <el-tree
            :data="dtList"
            :props="dtListTreeProps"
            node-key="id"
            ref="dtParentTree"
            @current-change="dtListTreeCurrentChangeHandle"
            :default-expand-all="true"
            :highlight-current="true"
            :expand-on-click-node="false"
          ></el-tree>
        </el-popover>
        <el-input
          v-model="dataForm.parentName"
          v-popover:dtListPopover
          :readonly="true"
          placeholder="点击选择上级单位"
        ></el-input>
      </el-form-item>
      <el-form-item label="编码" prop="code">
        <el-input v-model="dataForm.code" placeholder="编码"></el-input>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="dataForm.name" placeholder="名称"></el-input>
      </el-form-item>
      <el-form-item label="联系人" prop="linkman">
        <el-input v-model="dataForm.linkman" placeholder="联系人"></el-input>
      </el-form-item>
      <el-form-item label="联系电话" prop="phone">
        <el-input v-model="dataForm.phone" placeholder="联系电话"></el-input>
      </el-form-item>
      <el-form-item label="开始时间" prop="startDate">
        <el-date-picker
          v-model="dataForm.startDate"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endDate">
        <el-date-picker
          v-model="dataForm.endDate"
          type="date"
          placeholder="选择日期"
          format="yyyy-MM-dd"
        ></el-date-picker>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { treeDataTranslate } from "@/utils"
export default {
  data() {
    return {
      visible: false,
      isRoot: false,
      dataForm: {
        id: 0,
        parentId: "",
        parentName: "",
        code: "",
        name: "",
        linkman: "",
        phone: "",
        email: "",
        message: "",
        wechat: "",
        startDate: "",
        endDate: "",
        orderNum: "0",
        delFlag: "0"
      },
      dataRule: {
        parentName: [
          { required: true, message: "上级单位为空", trigger: "blur" }
        ],
        code: [{ required: true, message: "编码不能为空", trigger: "blur" }],
        name: [{ required: true, message: "名称不能为空", trigger: "blur" }],
        linkman: [
          { required: true, message: "联系人不能为空", trigger: "blur" }
        ],
        phone: [
          { required: true, message: "联系电话不能为空", trigger: "blur" }
        ],
        startDate: [
          { required: true, message: "开始结束时间不能为空", trigger: "blur" }
        ],
        endDate: [
          { required: true, message: "开始结束时间不能为空", trigger: "blur" }
        ]
      },
      dtList: [],
      dtListTreeProps: {
        label: "name",
        children: "children"
      }
    }
  },
  methods: {
    init(id) {
      this.isRoot = false
      this.dataForm.id = id || 0
      this.$http({
        url: this.$http.adornUrl("/dt/dt/list"),
        method: "get",
        params: this.$http.adornParams({
          page: 1,
          limit: 100000
        })
      })
        .then(({ data }) => {
          this.dtList = treeDataTranslate(data.page.list, "id")
        })
        .then(() => {
          this.visible = true
          this.$nextTick(() => {
            this.$refs["dataForm"].resetFields()
            // 修改
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/dt/dt/info/${this.dataForm.id}`),
                method: "get",
                params: this.$http.adornParams()
              }).then(({ data }) => {
                if (data && data.code === 0) {
                  this.dataForm.parentId = data.dt.parentId
                  // 根目录
                  if (this.dataForm.parentId === 0) {
                    this.isRoot = true
                  } else {
                    this.isRoot = false
                  }
                  this.dataForm.parentName = data.dt.parentName
                  this.dataForm.code = data.dt.code
                  this.dataForm.name = data.dt.name
                  this.dataForm.linkman = data.dt.linkman
                  this.dataForm.phone = data.dt.phone
                  this.dataForm.email = data.dt.email
                  this.dataForm.message = data.dt.message
                  this.dataForm.wechat = data.dt.wechat
                  this.dataForm.startDate = data.dt.startDate
                  this.dataForm.endDate = data.dt.endDate
                  this.dataForm.orderNum = data.dt.orderNum
                  this.dataForm.delFlag = data.dt.delFlag
                }
                this.dtListTreeSetCurrentNode()
              })
            }
          })
        })
    },
    // 分销商树选中
    dtListTreeCurrentChangeHandle(data, node) {
      this.dataForm.parentId = data.id
      this.dataForm.parentName = data.name
    },
    // 分销商树设置当前选中节点
    dtListTreeSetCurrentNode() {
      this.$refs.dtParentTree.setCurrentKey(this.dataForm.parentId)
    },
    // 表单提交
    dataFormSubmit() {
      this.$refs["dataForm"].validate(valid => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(
              `/dt/dt/${!this.dataForm.id ? "save" : "update"}`
            ),
            method: "post",
            data: this.$http.adornData({
              id: this.dataForm.id || undefined,
              parentId: this.dataForm.parentId,
              code: this.dataForm.code,
              name: this.dataForm.name,
              linkman: this.dataForm.linkman,
              phone: this.dataForm.phone,
              email: this.dataForm.email,
              message: this.dataForm.message,
              wechat: this.dataForm.wechat,
              startDate: this.dataForm.startDate,
              endDate: this.dataForm.endDate,
              orderNum: this.dataForm.orderNum,
              delFlag: this.dataForm.delFlag
            })
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.visible = false
                  this.$emit("refreshDataList")
                }
              })
            } else {
              this.$message.error(data.msg)
            }
          })
        }
      })
    }
  }
}
</script>
